<template>
	<view class="login_img"></view>
</template>

<script setup lang="ts">
	import { setToken } from '@/stores/token';
	import { TestIem } from '@/gql/user';
	import { useUserStore } from '@/stores/user';
	import { errorToast, showLoading, showToast, successToast } from '@/utils/prompt';
	import { onLoad } from '@dcloudio/uni-app';
	import { toPublish } from '@mqtt';
	import { encrypt } from '@/utils/jsencrypt';
	import { getDriverID, setDriverID } from '@/stores/driverID';
	import { toConfig } from '@/utils/wechat.jssdk';
	import { useUserInformation } from '@/stores/earthPushing/userSale';
	let type = ''; //类型
	let userInfor = null; //用户信息
	const userInfo = useUserStore();
	const userInfoSale = useUserInformation();
	onLoad((options) => {
		uni.removeStorageSync('storage_key');
		uni.removeStorageSync('driver_ID');
		type = options.type;
		if (options.type) {
			uni.setStorageSync('StoreData', options)
		}
		//加注机ID
		if (options.fill_id) {
			uni.setStorageSync('fillingID', options.fill_id);
		}
		Wechat();
	});
	/**
	 * 微信公众号登录
	 */
	function Wechat() {
		const ua = navigator.userAgent.toLowerCase(); //获取浏览器标识并转换为小写
		const isWechat = /MicroMessenger/i.test(ua); //判断是否微信返回布尔值
		if (isWechat) {
			const code = sessionStorage.getItem('code');
			if (code !== null) {
				const payload = {
					code,
				};
				toPublish('driver/mpLogin', payload, loginHanlder);
				sessionStorage.removeItem('code');
				return;
			}
			if (location.href.indexOf('code=') != -1) {
				showLoading('正在登录...');
				let code = location.href.split('code=')[1].split('&')[0];
				sessionStorage.setItem('code', code);
				location.replace(location.origin);
			} else {
				sessionStorage.setItem('type', type);
				const uri = encodeURIComponent(location.href);
				const pageUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx15f8f0a088eca852&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=${type}&connect_redirect=1#wechat_redirect`;
				location.href = pageUrl;
			}
		} else {
			alert('请在微信浏览器打开')
		}
	}

	/**
	 * 接收登录信息
	 */
	function loginHanlder(obj : any) {
		const { code, msg, data } = obj;
		if (code === 1) {
			sessionStorage.setItem('token', data.token);
			toConfig();
			setToken(data.token);
			const id = getDriverID();
			if (id !== data.id) {
				setDriverID(data.id);
			}
			UserIdentity(data.id);
			userInfo.getUserInfo();
			userInfoSale.getUserInfo();
			uni.setStorageSync('storage_key', data);
			uni.setStorageSync('driver_ID', data.id);
			userInfor = data;
			uni.setStorageSync('is_checker', data.role_count);
			successToast(msg);
			//判断进入的页面
			loginJudge();
		} else {
			errorToast(msg);
		}
	}
	function UserIdentity(id : number) {
		const payload = {
			query: TestIem,
			variables: {
				driver_id: id,
			},
		};
		toPublish('ql/driver/checkPage', payload, (obj : any) => {
			const { data } = obj;
			uni.setStorageSync('Useridentity', data.check_page.is_open);
		});
	}
	/**
	 * 判断进入的页面
	 */
	function loginJudge() {
		var StoreData = uni.getStorageSync('StoreData')
		console.log(typeof (StoreData))
		if (StoreData && typeof (StoreData) != 'string') {
			StoreData.driver_id = userInfor.id
		} else {
			StoreData = {
				type: ''
			}
		}
		toPublish('driver/getPages', StoreData, (obj : any) => {
			const { data, code, msg } = obj;
			const js = JSON.stringify(data.params)
			uni.setStorageSync('StoreData', encrypt(js))
			if (code === 1) {
				if (userInfor.do_order_id && data.params.type == 'filling') {
					PromptText('OrderProgres', '您有一个订单正在进行中', userInfor.do_order_id);
				} else {
					uni.redirectTo({
						url: data.page
					});
				}
			} else {
				errorToast(msg)
			}
		});
	}

	/**
	 * 提示文字并跳转页面
	 */
	function PromptText(type, text, num) {
		showToast(text);
		setTimeout(() => {
			uni.reLaunch({
				url: '/pages/canCode/canState?id=' + num + '&type=1',
			});
		}, 600);
	}
</script>

<style scoped lang="less">
	.login_img {
		width: 100%;
		height: 100vh;
		background-image: url(https://wx.anlan.tech/uploads/20220711/66d8a4a1e14430bd4e006ea801d44cd2.jpg);
		background-repeat: no-repeat;
		background-position: center;
		background-size: cover;
	}

	.VerifyLogin {
		width: 100%;
		height: 100vh;
		background-image: url(https://wx.anlan.tech/uploads/20220823/608fa7b7d1b4959125c61b9dbb4ae808.gif);
		background-repeat: no-repeat;
		background-position: center;
		background-size: cover;
	}
</style>